

var mongoose = require('mongoose'),
    sys = require('sys'),
    db = mongoose.connect('mongodb://localhost/p2p12'),
    Schema = mongoose.Schema,
 
PeerSchema = new Schema({
    address : String,
    port 	: String,
    auth 	: String
}),

BlockSchema = new Schema({
	peer 	: [Schema.Types.ObjectId],
	order 	: Number
})

FileSchema = new Schema({
    name       	: String,
    size       	: Number,
    blocks 		: [BlockSchema]
});	
    
module.exports = 
{
  Peer : mongoose.model('Peer', PeerSchema),

  register: function(peer)
  {
    var Peer = this.Peer;

    Peer.findOne({ port: peer.port }, function(err, item)
    {
        if(item)
        {
            sys.puts("Keepin' alive:" + JSON.stringify(peer));

            sys.puts(JSON.stringify(item));
            sys.puts(JSON.stringify(peer));

            item.address = peer.address;
            item.port = peer.port;

            item.update();
        }
        else
        {
            sys.puts("Registrin':" + JSON.stringify(peer));

            var p = new Peer({
                port    : peer.port,
                address : peer.address
            });

            p.save();
        }
    });
  },

  upload: function(file[])
  {
   
  }
}